﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommonService;
using QQZoneMarketingService;
using Spider.Service;
using Timer = System.Windows.Forms.Timer;

namespace QQMarketingSys
{
    public partial class Form1 : Form
    {
        private static int ReviveTimes { get; set; }//复活次数
        private static int ReviveIntervalTime { get; set; }//复活间隔时间，小时
        private Timer timerforrefresh;
        private Timer timerforTask;
        private static string LastVerifyCode;
        public Form1()
        {
            InitializeComponent();
            timerforrefresh = new Timer();
            timerforrefresh.Interval = 500;
            timerforrefresh.Tick += new EventHandler(timerforrefresh_Tick);
            timerforrefresh.Start();
            timerforTask = new Timer();
            timerforTask.Interval = 2000;
            timerforTask.Tick += new EventHandler(timerforTask_Tick);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtVerifilCode.AutoSize = false;
            webBrowser1.Navigate("http://user.qzone.qq.com");
            nudqqAcountMaxVisite.Value = QQVisiteService.GetCurrentMaxVisiteTimes();
            txtVisiteUrl.Text = "827756467";
            UUYunService.Uu_setSoftInfo();
            var info1 = UUYunService.Uu_login();
            if (info1 < 0)
                MessageBox.Show("UU云登录失败，错误代码：" + info1);
            SpiderService.SetQqSpriderAutoWritVerifyCode(true);
            txtVerifilCode.Enabled = false;
            llbChangeCode.Enabled = false;
            SpiderService.SetQqSpriderVisiteDestinationUrl(txtVisiteUrl.Text);
            SpiderService.SetQqSpriderEnableTimeVisit(cbEnnableTimeVisit.Checked);
            SpiderService.SetQqSpriderAutoWritVerifyCodeErrorTimes((int)nudMaxVerifyCOdeErrorTimes.Value);
            ThreadPool.SetMaxThreads((int)nudThreadpool.Value, (int)nudThreadpool.Value);
            SpiderService.SetSpriderCaptainWorkSpeed((int)nupSpriderWorkSpeed.Value * 1000);
            ReviveTimes = (int)nudSystemReveTimes.Value;
            ReviveIntervalTime = (int)nudSystemReveInterval.Value;
            SpiderService.SetSpriderWebBrowserDelay((int)nudWebrowserDelay.Value);
            SpiderService.SetQqSpriderApplyVisiteZone(cbApplyVisiteZone.Checked);
            SpiderService.SetQqSpriderZoneAddFriend(cbZoneAddFriend.Checked);
            SpiderService.SetQqSpriderZoneShuoShuoSayGood(cbShuoshuoSayGood.Checked);
            SpiderService.SetQqSpriderZoneIndexSayGood(cbZoneIndexSayGood.Checked);
            SpiderService.SetQqSpriderCheckZoneLogsAvaliable(cbCheckAdAvaliable.Checked);
            SpiderService.SetQqSpriderZoneMessageBorderLiveMessage(cbZoneLiveMessage.Checked);
            SpiderService.SetQqSpriderEnableZoneLogsSayGood(cbZoneLogSayGood.Checked);
            QQVisiteService.AutoUpdateQQAcountHasVisiteTimes();

        }
        private void timerforrefresh_Tick(object sender, EventArgs e)
        {
            lblSpriderRemainTask.Text = SpiderMonitor.GetTodoTaskCount().ToString();
            lblAntRemainTask.Text = SpiderMonitor.RemainAntTask.ToString();
            lblAntFinishedCount.Text = SpiderMonitor.GetVisitedCount().ToString();
            lblSpriderWorking.Text = SpiderMonitor.SpiderIsWorking ? "是" : "否";
            lblAntWorking.Text = SpiderMonitor.AntIsWorking ? "是" : "否";
            btnSpriderStartToWork.Text = SpiderMonitor.SpiderIsWorking ? "停止工作" : "开始工作";
            btnAntStartToWork.Text = SpiderMonitor.AntIsWorking ? "停止工作" : "开始工作";
            lblSpriderCaptainreason.Text = SpiderMonitor.SpiderCaptainStopWorkReason;
            lblAntCaptainReason.Text = SpiderMonitor.AntCaptainStopWorkReason;
            lblAntRemainCount.Text = SpiderMonitor.RemainAntTask.ToString();
            lblAntSuccessFinishedCount.Text = SpiderMonitor.AntSuccessFinishedTaskCount.ToString();
            lblAntFinishedCount.Text = SpiderMonitor.AntFinishedTaskCount.ToString();
            lblSpiderCaptainStartTime.Text = SpiderMonitor.SpiderCaptainStartWorkTime.ToString();
            lblSpiderCaptainEndTime.Text = SpiderMonitor.SpiderCaptainEndWorkTime.ToString();
            lblRemainReviewTimes.Text = ReviveTimes.ToString();
            if (SpiderMonitor.SpiderIsWorking && SpiderMonitor.AntIsWorking)
            {
                btnStart.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            }
            else
            {
                btnStart.BackColor = System.Drawing.Color.Red;
            }
            if (SpiderMonitor.AntFinishedTaskCount != 0)
            {
                lblAntSuccesRate.Text =
              (Math.Round(((double)SpiderMonitor.AntSuccessFinishedTaskCount / (double)SpiderMonitor.AntFinishedTaskCount), 2) * 100) + "%";
            }
        }
        private void timerforTask_Tick(object sender, EventArgs e)
        {
            QQVisiteService.AutoAddWaitVisitedQQNumberForZone((int)nudTaskCount.Value);
            var img = SpiderMonitor.GetQqZoneSpriderVerifyCodeImage;
            if (img != null)
            {
                pictureBox1.Image = img;
            }
            //系统自动重启
            if (ReviveTimes > 0 && SpiderMonitor.SpiderCaptainEndWorkTime != DateTime.MinValue && DateTime.Now - SpiderMonitor.SpiderCaptainEndWorkTime > new TimeSpan(0, 0, ReviveIntervalTime, 0))
            {
                btnStart_Click(null,null);
                ReviveTimes--;
            }
        }
        private void nudThreadpool_ValueChanged(object sender, EventArgs e)
        {
            ThreadPool.SetMaxThreads((int)nudThreadpool.Value, (int)nudThreadpool.Value);
        }

        private void nupSpriderWorkSpeed_ValueChanged(object sender, EventArgs e)
        {
            SpiderService.SetSpriderCaptainWorkSpeed((int)nupSpriderWorkSpeed.Value * 1000);
        }

        private void btnSpriderStartToWork_Click(object sender, EventArgs e)
        {
            if (btnSpriderStartToWork.Text == "开始工作")
                SpiderService.CommandSpiderToWorkBySTA(webBrowser1);
            else
            {
                SpiderService.CommandSpiderStopToWork();
            }

        }

        private void btnAntStartToWork_Click(object sender, EventArgs e)
        {
            if (btnAntStartToWork.Text == "开始工作")
                AntService.CommandAntCaptainToWork();
            else
            {
                AntService.CommandAntCaptainStopToWork();
            }
        }
      
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (cbEnnableTimeVisit.Checked)
                QQVisiteService.AutoAddWaitVisitedQQNumberForZone((int)nudTaskCount.Value);
            else
            {
                QQVisiteService.AutoAddWaitVisitedQQNumberForZone((int)nudTaskCount.Value);
            }
            this.BeginInvoke((ThreadStart)delegate() { QQVisiteService.StartToQQZoneVisite(webBrowser1); });
            timerforTask.Start();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void nudWebrowserDelay_ValueChanged(object sender, EventArgs e)
        {
            SpiderService.SetSpriderWebBrowserDelay((int)nudWebrowserDelay.Value);
        }

        private void nudqqAcountMaxVisite_ValueChanged(object sender, EventArgs e)
        {
            QQVisiteService.ReSetAllQQAcountMaxVisiteTimes0((int)nudqqAcountMaxVisite.Value);
        }
        private void txtVerifilCode_KeyUp(object sender, KeyEventArgs e)
        {
            string code = txtVerifilCode.Text;
            if (code.Length == 4)
            {
                SpiderService.SetQqSpriderVerifyCode(code);
                txtVerifilCode.Text = "";
                pictureBox1.Image = null;
            }
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (LastVerifyCode != null)
                pictureBox1.Image = Image.FromStream(System.Net.WebRequest.Create(LastVerifyCode).GetResponse().GetResponseStream());
        }

        private void llbChangeCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SpiderService.ChangeVerifyCode();
        }

        private void cbEnnableTimeVisit_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderEnableTimeVisit(cbEnnableTimeVisit.Checked);
        }

        private void cbVisiteDestinationUrl_CheckedChanged(object sender, EventArgs e)
        {
            if (cbVisiteDestinationUrl.Checked)
            {
                SpiderService.SetQqSpriderVisiteDestinationUrl(txtVisiteUrl.Text);
            }
            else
            {
                SpiderService.SetQqSpriderVisiteDestinationUrl(null);
            }
        }

        private void cbAutoVerifyCode_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderAutoWritVerifyCode(cbAutoVerifyCode.Checked);
            if (cbAutoVerifyCode.Checked)
            {
                txtVerifilCode.Enabled = false;
                llbChangeCode.Enabled = false;
            }
            else
            {
                txtVerifilCode.Enabled = true;
                llbChangeCode.Enabled = true;
            }
        }

        private void nudMaxVerifyCOdeErrorTimes_ValueChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderAutoWritVerifyCodeErrorTimes((int)nudMaxVerifyCOdeErrorTimes.Value);
        }

        private void cbApplyVisiteZone_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderApplyVisiteZone(cbApplyVisiteZone.Checked);
        }

        private void cbZoneAddFriend_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderZoneAddFriend(cbZoneAddFriend.Checked);
        }

        private void cbShuoshuoSayGood_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderZoneShuoShuoSayGood(cbShuoshuoSayGood.Checked);
        }

        private void cbZoneIndexSayGood_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderZoneIndexSayGood(cbZoneIndexSayGood.Checked);
        }

        private void btnVisiteDestinationURL_Click(object sender, EventArgs e)
        {
            QQVisiteService.AddURLtoTaskQueen(txtVisiteUrl.Text);
        }

        private void cbCheckAdAvaliable_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderCheckZoneLogsAvaliable(cbCheckAdAvaliable.Checked);
        }

        private void cbZoneLiveMessage_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderZoneMessageBorderLiveMessage(cbZoneLiveMessage.Checked);
        }

        private void cbZoneLogSayGood_CheckedChanged(object sender, EventArgs e)
        {
            SpiderService.SetQqSpriderEnableZoneLogsSayGood(cbZoneLogSayGood.Checked);
        }

        private void nudSystemReveTimes_ValueChanged(object sender, EventArgs e)
        {
            ReviveTimes = (int)nudSystemReveTimes.Value;
        }

        private void nudSystemReveInterval_ValueChanged(object sender, EventArgs e)
        {
            ReviveIntervalTime = (int)nudSystemReveInterval.Value;
        }

    }
}
